<?php
// $Id$

/**
 * Controller_Columns 控制器
 */
class Controller_Columns extends Controller_Abstract
{



    function actionIndex()
    {
        // 为 $this->_view 指定的值将会传递数据到视图中
        # $this->_view['text'] = 'Hello!';
    }


    function actionMyColumns() {
        $user = Helper_RBAC::get('user');
        if(!in_array('ADMIN', Helper_RBAC::roles('user'))) {
            //            /*
            foreach($user->department->call_columns as $key=>$column) {
                $nodes[] = $column->col_id;
                //                FUNC::getChildren(Column::getFormCache(null, $column->col_id), $nodes, array('col_id'), false);
            }

            foreach($user->roles as $key=>$role) {
                foreach($role->call_columns as $key=>$column) {
//                    FUNC::getChildren(Column::getFormCache(null, $column->col_id), $nodes, array('col_id'), false);    //查找孩子
                    $nodes[] =  $column->col_id;
                }
            }

            //            $orCondi = new QDB_Expr(
            //            'col_id in('. implode(',', $nodes) . ')'
            //                    );
            //                dump($nodes);
            if($nodes) {
                $condi[] = new QDB_Expr(
                        'col_id in('. implode(',', $nodes) . ')'
                );
            }
            else {
                $condi[] = new QDB_Cond(
                    'parent_id = ?',0
                );
            }

            //             */

/*


            if($nodes) {
                if($cols) {
                    $condi[] = new QDB_Expr(
                        'col_id in('. implode(',', array_merge($nodes, $cols)) . ')'
                    );
                }
                else {
                    $condi[] = new QDB_Expr(
                        'col_id in('. implode(',', $nodes) . ')'
                    );
                }
            }
            $parent_id = intval($this->_context->parent_id);
            $condi[] = new QDB_Cond(
                  'parent_id = ?', $parent_id
            );
*/
        }
        else {
            $condi[] = new QDB_Cond(
              'parent_id = ?', 0
            );
        }
        //                                        dump($condi);
        //        $parent_id = intval($this->_context->parent_id);
        $result = Column::find($condi)->all()->query()->toArray();
        return json_encode($result);
    }

    /**
     * 返回栏目JSON数据
     */
    function actionDumpParent()
    {
        $user = Helper_RBAC::get('user');
        //        dump();
        if(!in_array('ADMIN', Helper_RBAC::roles('user')) && FUNC::getV('hasrole')) {
            //            FUNC::getChildren(Column::getFormCache(null, $this->_context->selected), $childrens, array('col_id'), false);    //查找孩子
            //            $childrens[] = $this->_context->selected;

/*
            foreach($user->roles as $key=>$role) {
                foreach($role->call_columns as $key=>$column) {
                    FUNC::getChildren(Column::getFormCache(null, $column->col_id), $nodes, array('col_id'), false);    //查找孩子
                    $nodes[] =  $column->col_id;
                    //                    FUNC::getParent(Column::getFormCache(), $column->col_id, $nodes, array('col_id'), false);    //查找父亲
                    //                FUNC::getParent(Column::getFormCache(null, $column->col_id), $nodes, array('col_id'), false);
                    //               $cols[] = $column->col_id;
                    //dump($nodes);
                }
            }
 *
 */
            //dump($childrens);
            //            if($childrens) {
            //                $orCondi[] = new QDB_Expr(
            //            'parent_id in(' .implode(',', $childrens). ')'
            //                );
            //            }
            ///*
//            foreach($user->department->call_columns as $key=>$column) {
//                $nodes[] = $column->col_id;
//                FUNC::getChildren(Column::getFormCache(null, $column->col_id), $nodes, array('col_id'), false);
//            }
            //*/
            //            $orCondi = new QDB_Expr(
            //            'col_id in('. implode(',', $nodes) . ')'
            //                    );


            //                dump($nodes);
            if($nodes) {
                if($cols) {
                    $condi[] = new QDB_Expr(
            'col_id in('. implode(',', array_merge($nodes, $cols)) . ')'
                    );
                }
                else {
                    $condi[] = new QDB_Expr(
            'col_id in('. implode(',', $nodes) . ')'
                    );
                }
            }
            else {
                if($cols) {
                    $condi[] = new QDB_Expr(
                    'col_id in('. implode(',', $cols) . ')'
                    );
                }
            }
        }
        //        else {
        //           $condi[] = new QDB_Cond(
        //                        '[fields.hidden] = ?', 0
        //                    );
        //        }


        //                        dump($condi);
        $parent_id = intval($this->_context->parent_id);
        $result = Column::find('parent_id = ?', $parent_id)->where($condi)->all()->query()->toArray();
        //$result = Column::getFormCache(null, $parent_id);
        //dump($result);
        return json_encode($result);
    }

    //    function actionEdit() {
    //        $form = new Form_AddColumn(url('columns/save'));
    //        $this->_view['form'] = $form;
    //        $this->_viewname = 'edit';
    //    }

}


